﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Mvc;

using PortalIntranet.BusinessComponents.Administracao;
using PortalIntranet.DatabaseComponents.Administracao;
using PortalIntranet.InternalComponents.Security;
using IntranetApplication.Models.Dynamic;
using PortalIntranet.InternalComponents.Configuration;
using PortalIntranet.BusinessComponents.WebParts;
using PortalIntranet.DatabaseComponents.WebParts;
using PortalIntranet.ObjectComponents.Administracao;
using PortalIntranet.ObjectComponents.WebParts;
using PortalIntranet.ObjectComponents.Comun;

namespace IntranetApplication.Controllers.Dynamic
{
    public class DynamicController : Controller
	{
		#region Index
		[Authorize]
		public ActionResult Index(string slug, string wpg)
        {
			string[] uri = slug.Split('/');
			Departamento departamento = null;
			WebPage webpage = null;
			DynamicModel dyn = null;
			bool home = false;

			if (slug.ToLower() == "home")
			{
				departamento = DepartamentosBll.ObterHome(((CustomPrincipal)HttpContext.User).Empresa.Id);
				home = true;
			}
			else
			{
				departamento = DepartamentosBll.Obter(uri[uri.Length - 1], ((CustomPrincipal)HttpContext.User).Empresa.Id);
			}

			if (departamento == null) return RedirectToAction("Error404", "Error");

			if (home)
			{
				webpage = WebPagesBll.Obter("home", departamento.Id);
				if (webpage == null) return RedirectToAction("Error404", "Error");
				dyn = PrepararModeloHome(departamento, webpage, wpg);
			}
			else
			{
				webpage = WebPagesBll.Obter(wpg, departamento.Id);
				if (webpage == null) return RedirectToAction("Error404", "Error");
				dyn = PrepararModelo(departamento, webpage, wpg);
			}

            return View(dyn);
        }
		#endregion

		#region Actions
		[Authorize]
		[HttpPost]
		public ActionResult AtualizarLocalizacao(string Localizacao, string Url)
		{
			var js = new JavaScriptSerializer();
			List<WebPartLocalizacaoInfo> portlets = null;

			if (!string.IsNullOrEmpty(Localizacao))
			{
				portlets = (List<WebPartLocalizacaoInfo>)js.Deserialize(Localizacao, typeof(List<WebPartLocalizacaoInfo>));
			}

			if (portlets != null)
			{
				portlets.ForEach(p => WebPartsBll.AtualizarLocalizacao(p));
			}

			return Json(
				new
				{
					messages = string.Empty,
					url = Url
				},
				"application/json"
			);
		}
		#endregion

		#region Functions
		public DynamicModel PrepararModeloHome(Departamento departamento, WebPage webpage, string wpg)
		{
			DynamicModel model = new DynamicModel();
			model.Nome = webpage.Nome;
			model.Uri = new UriInfo("Home", "Home");
			model.WebPages = ObterWebPages(departamento.Id, wpg);
			model.Portlets = ObterPortlets(webpage.TipoLayoutId);
			model.WebParts = ObterWebParts(departamento, webpage.Id);

			return model;
		}

		public DynamicModel PrepararModelo(Departamento departamento, WebPage webpage, string wpg)
		{
			DynamicModel model = new DynamicModel();
			model.Nome = webpage.Nome;
			model.Uri = DepartamentosBll.ObterUri(departamento);
			model.WebPages = ObterWebPages(departamento.Id, wpg);
			model.Portlets = ObterPortlets(webpage.TipoLayoutId);
			model.WebParts = ObterWebParts(departamento, webpage.Id);

			return model;
		}

		public List<WebPageInfo> ObterWebPages(int departamento, string wpg)
		{
			List<WebPage> lista = WebPagesBll.ListarWebPages(departamento);
			List<WebPageInfo> webpages = new List<WebPageInfo>();

			foreach (var pg in lista)
			{
				webpages.Add(new WebPageInfo(pg, (string.IsNullOrWhiteSpace(wpg) ? false : (pg.Slug.ToLower() == wpg.ToLower()))));
			}

			return webpages;
		}

		public List<PortletInfo> ObterPortlets(int tipolayout)
		{
			List<Portlet> lista = PortletsBll.ListarPortlets(tipolayout);
			List<PortletInfo> portlets = new List<PortletInfo>();

			foreach (var pt in lista)
			{
				portlets.Add(new PortletInfo(pt));
			}

			return portlets;
		}

		public List<WebPartBaseInfo> ObterWebParts(Departamento departamento, int webpage)
		{
			List<WebPart> lista = WebPartsBll.ListarWebParts(webpage);
			List<WebPartBaseInfo> webparts = new List<WebPartBaseInfo>();

			foreach (var wp in lista)
			{
				webparts.Add(WebPartsBll.ConverterWebPart(departamento, wp));
			}

			return webparts;
		}
		#endregion
    }
}
